package com.juku2024.juku.widget

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.animation.ObjectAnimator
import android.view.ViewGroup
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import androidx.constraintlayout.widget.ConstraintLayout
import com.rabbit.videoplayer.MixPlayerView
import com.juku2024.juku.R
import com.juku2024.juku.tools.LogC



class VideoScreenViewNew(context: Context, attributeSet: AttributeSet) : ConstraintLayout(context, attributeSet) {

    // 是否投屏正在播放
    private var isPlaying = false
    // 是否正在显示投屏View
    private var isShowingDes = false
    // 是否连接上设备
    private var isConnectDevices = false
    // 播放地址
    private var url: String? = null

    private val dlanRoot: ViewGroup

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.view_video_screen_new, this, true)
        view.setBackgroundColor(Color.BLACK)
        view.isFocusable = true
        view.isClickable = true

        dlanRoot = view.findViewById(R.id.dlanRoot)
        
        val animator: ObjectAnimator = ObjectAnimator.ofFloat(this, "translationY", 0f, 2000f)
        animator.duration = 1
        animator.interpolator = AccelerateInterpolator()
        animator.start()
    }

    private var  videoPlayer: MixPlayerView? = null
    fun showScreen(videoPlayer: MixPlayerView) {
        this.videoPlayer = videoPlayer
        this.url = videoPlayer.getVodUrl()
        dlanRoot.removeAllViews()
//        LelinkSourceSDK.getInstance()
//            .setSdkInitInfo(context , "21742", "67016cb27520aef377d452e276b14239")
//            .easyPush(dlanRoot)
//        LelinkSourceSDK.getInstance().setEasyCastListener(this)
        isConnectDevices = true
        show()
    }

//
//    /**
//     * 如果没有触发此回调，则表示此次未搜索到设备或用户未投屏
//     * @param lelinkServiceInfo
//     * @return
//     */
//    override fun onCast(p0: LelinkServiceInfo?): EasyCastBean {
//        LogC.e("投屏 -> onCast")
//        isConnectDevices = true
//        val bean = EasyCastBean()
//        bean.url = url //实际需要投屏的url
//        videoPlayer?.showCastScreenConnected()
//
//        return bean
//    }
//
//    /**
//     * 如果投屏失败，在搜索页面关闭的时候，会触发回调
//     * @param lelinkServiceInfo
//     * @param easyCastBean
//     * @param what
//     * @param extra
//     */
//    override fun onCastError(p0: LelinkServiceInfo, p1: EasyCastBean, p2: Int, p3: Int) {
//        isPlaying = false
//        isConnectDevices = false
//
//        videoPlayer?.showCastScreenDisConnect("投屏失败，请重试！")
//    }
//
//    /**
//     * 推送成功
//     * @param lelinkServiceInfo
//     * @param easyCastBean
//     */
//    override fun onCastLoading(p0: LelinkServiceInfo, p1: EasyCastBean?) {
//        videoPlayer?.showCastScreenDisConnect(p0.name)
//    }
//    /**
//     * 暂停
//     * @param lelinkServiceInfo
//     * @param easyCastBean
//     */
//    override fun onCastPause(p0: LelinkServiceInfo?, p1: EasyCastBean?) {
//        isPlaying = false
//        videoPlayer?.caseScreenSwitchPlayOrPause(isPlaying)
//    }
//
//    override fun onCastStart(p0: LelinkServiceInfo?, p1: EasyCastBean?) {
//        isPlaying = true
//        videoPlayer?.caseScreenSwitchPlayOrPause(isPlaying)
//    }
//
//    /**
//     * 收端播放结束，如果需要实现下一集，可在之后的onCast回调中提供播放信息
//     * @param lelinkServiceInfo
//     * @param easyCastBean
//     */
//    override fun onCastCompletion(p0: LelinkServiceInfo?, p1: EasyCastBean?) {
//        isPlaying = false
//        videoPlayer?.caseScreenSwitchPlayOrPause(isPlaying)
//    }
//
//    override fun onCastPositionUpdate(p0: LelinkServiceInfo?, p1: EasyCastBean?, p2: Long, p3: Long) {
//        videoPlayer?.updateCastScreenCurrentPosition(p3 * 1000)
//    }
//    /**
//     * 搜索退出播放，不同于 onCastCompletion，此时需要恢复APP视频播放
//     * @param lelinkServiceInfo
//     * @param easyCastBean
//     */
//    override fun onCastStop(p0: LelinkServiceInfo?, p1: EasyCastBean?) {
//        isPlaying = false
//        videoPlayer?.caseScreenSwitchPlayOrPause(isPlaying)
//    }
//
//    override fun onDismiss() {
//        LogC.e("投屏 -> onDismiss....")
//    }


    private fun show() {
        isShowingDes = true
        val animator: ObjectAnimator = ObjectAnimator.ofFloat(this, "translationY", 2000f, 0f)
        animator.duration = 200
        animator.interpolator = DecelerateInterpolator()
        animator.start()
    }

    fun isShowing(): Boolean {
        return isShowingDes
    }

    fun hide() {
        if (!isShowingDes) return
        isPlaying = false
        isShowingDes = false
        isConnectDevices = false
        val animator: ObjectAnimator = ObjectAnimator.ofFloat(this, "translationY", 0f, 2000f)
        animator.duration = 200
        animator.interpolator = AccelerateInterpolator()
        animator.start()
        release()
    }


    fun release(){
        LogC.e("DLNAManager -> 释放...")
        try {
//            LelinkSourceSDK.getInstance().unBindSdk()
        } catch (e:Exception) {
            e.printStackTrace()
        }
    }

}